/*******************************************************************************
 *
 *               Copyright(c) 2005 by 中国移动通信有限公司.
 *
 *                       All rights reserved.
 *
 *******************************************************************************
 *     File Name         :  com.flute.search.mark.SameRemoveSpecialMarkStrategy.java
 *     Description       :
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description
 *     0          Jun 23, 2010         TangHongJun              Created
 ******************************************************************************/

package com.flute.framework.search.highlight;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.flute.tools.util.StringUtil;

/**
 * @author TangHongJun
 * 
 *         vision 创建时间：Jun 23, 2010
 */
public class SameRemoveSpecialMarkStrategy implements IMarkStrategy {

	public SameRemoveSpecialMarkStrategy() {

	}

	public static void main(String args[]) {
		String keyword = "iseeyou";
		String word = "I See You";
		SameRemoveSpecialMarkStrategy py = new SameRemoveSpecialMarkStrategy();
		System.out.println(new SimpleMark().markString(py.getMarked(keyword,
				word)));
	}

	@Override
	public MarkPosition[] getMarked(String keyword, String searchString) {
		if (!checkSpecialChars(searchString) && !checkSpecialChars(keyword)) {
			return new MarkPosition[] {};
		}
		char[] chars = searchString.toCharArray();
		List<Integer> hash = new ArrayList<Integer>();
		int length = chars.length;
		int isTrueCount = 0;
		for (int i = 0; i < length; i++) {
			if (!checkSpecialChars(chars[i])) {
				hash.add(i);
				isTrueCount++;
			}
		}
		Integer[] ayh = hash.toArray(new Integer[] {});
		List<MarkPosition> list = new ArrayList<MarkPosition>();
		SameMarkStrategy py = new SameMarkStrategy();
		MarkPosition[] mps = py.getMarked(StringUtil
				.removeSpecialChars(StringUtil.getStandardString(keyword)),
				StringUtil.removeSpecialChars(StringUtil
						.getStandardString(searchString)));
		for (MarkPosition mp : mps) {
			MarkPosition markp = null;
			int start = mp.getStart();
			int sumEnd = mp.getEnd();
			while (start < sumEnd) {
				markp = new MarkPosition();
				int startMark = ayh[start];
				int end = ayh[start];
				int i = start + 1;
				for (; i < sumEnd; i++) {
					if ((end + 1) == ayh[i]) {
						end = ayh[i];
					} else {
						break;
					}
				}
				markp.setStart(startMark);
				markp.setEnd(end + 1);
				markp.setSource(searchString);
				list.add(markp);
				start = i;
			}
		}
		return list.toArray(new MarkPosition[] {});
	}

	private static boolean checkSpecialChars(char ch) {
		return checkSpecialChars(ch + "");
	}

	private static boolean checkSpecialChars(String str) {
		String regEx = "[`~!@#$%^&* ()_+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
		Matcher m = null;
		try {
			Pattern p = Pattern.compile(regEx);
			m = p.matcher(str);
		} catch (PatternSyntaxException p) {
			p.printStackTrace();
			return Boolean.FALSE;
		}
		if (m == null) {
			return Boolean.FALSE;
		}
		return m.find();
	}

}
